<?php

namespace App\Http\Controllers;

use App\Services\SmsService;
use Illuminate\Http\Request;
use App\Jobs\SmsSend;
use Illuminate\Support\Facades\Auth;

class SmsController extends SmsBaseController
{
    /**
     * @api {post} /api/sms/send 发送短信
     * @apiGroup Sms
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id 客户id
     * @apiParam {String} text 短信内容
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "message": "短信发送成功"
     *       }
     */
    public function send(Request $request,SmsService $smsService)
    {
        $validatedData = $request->validate([
            'id' => 'required|exists:mysql_branch.fs_user,id',
            'text' => 'required|max:60'
        ],[],[
            'id'=> '会员',
            'text'=> '内容'
        ]);
        $res = $smsService->sendMsm($validatedData);
        if ($res['status'] == false) {
            return response()->json(['error' => $res['msg']], 422);
        } else {
            return response()->json(['message' => '短信发送成功']);
        }
    }

    /**
     * @api {post} /api/sms/send/batch 批量发送短信
     * @apiGroup Sms
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} ids 客户id
     * @apiParam {String} text 短信内容
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "message": "短信发送成功"
     *       }
     */
    public function sendMore(Request $request,SmsService $smsService){
        $validatedData = $request->validate([
            'ids' =>'required|array',
            'ids.*' => 'exists:mysql_branch.fs_user,id',
            'text' => 'required|max:60'
        ],[],[
            'id'=> '会员',
            'text'=> '内容'
        ]);
        $validatedData['auth_id'] =  Auth::user()->id;
        $validatedData['auth_name'] = Auth::user()->name;
        dispatch(new SmsSend($validatedData));
        return response()->json(['message' => '群发短信推送队列中']);
    }
}
